package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.utils.Constant;

public class AlterPassword extends BaseFragmentActivity implements
		OnClickListener
{
	private EditText phone;
	// private EditText pass;
	private EditText pass1;
	private EditText pass2;
	private Button goBreak;
	private Button goAffirm;
	private TextView title;
	private TextView title1;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_alter_password);
		initViews();
		initEvents();
	}

	@Override
	protected void initViews()
	{
		// TODO Auto-generated method stub
		title = (TextView) findViewById(R.id.top_text_name);
		title1 = (TextView) findViewById(R.id.top_text_step);
		phone = (EditText) findViewById(R.id.reg_baseinfo_et_name);
		// pass = (EditText) findViewById(R.id.reg_baseinfo_et_pass1);
		pass1 = (EditText) findViewById(R.id.reg_baseinfo_et_pass2);
		pass2 = (EditText) findViewById(R.id.reg_baseinfo_et_pass3);
		goBreak = (Button) findViewById(R.id.btn_back);
		goAffirm = (Button) findViewById(R.id.btn_ok);
		phone.setEnabled(false);
		phone.setText("" + mApplication.user.getPhone());
		title.setText("密码修改");
		title1.setVisibility(View.GONE);
	}

	@Override
	protected void initEvents()
	{
		goBreak.setOnClickListener(this);
		goAffirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_ok:
			String tel = phone.getText().toString();
			String pa1 = pass1.getText().toString();
			String pa2 = pass2.getText().toString();
			if (tel.length() < 1)
			{
				showShortToast("请输入您的手机号码");
				break;
			} else if (tel.length() != 11)
			{
				showShortToast("请输入11位数的手机号码");
				break;
			}
			if (pa1.length() < 1)
			{
				showShortToast("请输入您的新密码");
				break;
			}
			if (pa2.length() < 1)
			{
				showShortToast("请确认您的新密码");
				break;
			}
			if (!pa1.equals(pa2))
			{
				showShortToast("两次输入的密码不一致");
				break;
			}
			alterPassword(tel, pa1);
			break;
		}
	}

	public void alterPassword(String phone, String pwd)
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "14");
		params.put("phone", phone);
		params.put("pwd", pwd);
		params.put("lat", "" + mApplication.mLatitude);
		params.put("lng", "" + mApplication.mLongitude);
		FinalHttp fh = new FinalHttp();
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
				showShortToast("密码修改失败，服务器异常");
			}

			@Override
			public void onSuccess(Object t)
			{
				super.onSuccess(t);
				String json = t.toString();
				if (json.contains("111111"))
				{
					showShortToast("密码错误，修改失败");
				} else
				{
					showShortToast("修改密码成功");
					startActivity(HomePageActivity.class);
				}
			}
		});
	}

	@Override
	public void onBackPressed()
	{
		finish();
	}
}
